﻿using UnityEngine;
using UnityEngine.EventSystems;

namespace Assets.AnimationPlayer.Prefabs
{
    public enum CursorSizeType
    {
        Size8X8 = 0,
        Size16X16
    }
    public class CursorStyle : EventTrigger
    {

        public Texture2D HoverTexture2D;
        private const CursorSizeType SizeType = CursorSizeType.Size8X8;

#if SizeType == Size8X8
        private readonly Vector2 _startPoint = new Vector2(12, 12);
#else 
    private readonly Vector2 startPoint = new Vector2(8, 9);
#endif



        // Use this for initialization
        internal void Start()
        {
#if sizeType == Size8X8
            HoverTexture2D = Resources.Load<Texture2D>("icons/cursor_hand8");
#else
        hoverTexture2D = Resources.Load<Texture2D>("icons/cursor_hand16");
#endif
            _setCursor(EventSystem.current.IsPointerOverGameObject());
        }


        public override void OnPointerEnter(PointerEventData eventData)
        {
            if (transform.Find("hoverPanel") != null)
            {
                transform.Find("hoverPanel").gameObject.SetActive(true);
            }

            _setCursor(true);
            base.OnPointerEnter(eventData);
        }

        public override void OnPointerExit(PointerEventData eventData)
        {
            if (transform.Find("hoverPanel") != null)
                transform.Find("hoverPanel").gameObject.SetActive(false);

            _setCursor(false);

            base.OnPointerExit(eventData);
        }


        private void _setCursor(bool isReplace)
        {
            if (isReplace)
            {
                Cursor.SetCursor(HoverTexture2D, _startPoint, CursorMode.Auto);
            }
            else
            {
                Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
            }
        }
    }
}